package com.naiterui.ehp.bs.ds.modules.statistics.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.support.advice.IgnoreResponseEntityWrap;
import com.naiterui.ehp.bp.support.utils.FileUtil;
import com.naiterui.ehp.bs.ds.common.controller.AbstractController;
import com.naiterui.ehp.bs.ds.common.utils.PageVO;
import com.naiterui.ehp.bs.ds.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.ds.modules.statistics.service.IOrderStatisticsService;
import com.naiterui.ehp.bs.ds.modules.statistics.service.ISkuStatisticsService;
import com.naiterui.ehp.bs.ds.modules.statistics.vo.OrderStatisticsVO;
import com.naiterui.ehp.bs.ds.modules.statistics.vo.SaleStatisticsParamVO;
import com.naiterui.ehp.bs.ds.modules.statistics.vo.SkuStatisticsVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @author guoyongxiang
 * @date 2019/10/11 10:35
 * @since 1.0.0
 */
@Slf4j
@Validated
@Api(tags = {SwaggerTag.DS + SwaggerTag.ORDER})
@RestController
@RequestMapping("statistics")
public class SaleStatisticsController extends AbstractController {

  @Autowired
  private IOrderStatisticsService orderStatisticsService;
  @Autowired
  private ISkuStatisticsService skuStatisticsService;

  @SysLog("订单统计")
  @GetMapping("/order/list")
  @ApiOperation("订单统计")
  @PreAuthorize("@p.check('sale:list')")
  public PageVO<OrderStatisticsVO> orderList(PageParamsVO pageParamsVO, SaleStatisticsParamVO sale) {
    return this.orderStatisticsService.queryPage(pageParamsVO, sale);
  }

  @SysLog("导出订单统计")
  @GetMapping("/order/export")
  @ApiOperation("导出订单统计")
  @IgnoreResponseEntityWrap
  @PreAuthorize("@p.check('sale:list')")
  public void exportOrder(PageParamsVO pageParamsVO, SaleStatisticsParamVO sale, HttpServletResponse response) throws IOException {
    byte[] body = this.orderStatisticsService.export(pageParamsVO, sale);
    String fileName = System.getProperty("java.io.tmpdir") + File.separator + System.currentTimeMillis() + ".xls";
    FileUtil.downloadFile(this.request, response, FileUtil.writeBytes(body, fileName), true);
  }

  @SysLog("商品统计")
  @GetMapping("/sku/list")
  @ApiOperation("商品统计")
  @PreAuthorize("@p.check('sale:list')")
  public PageVO<SkuStatisticsVO> skuList(PageParamsVO pageParamsVO, SaleStatisticsParamVO sale) {
    return this.skuStatisticsService.queryPage(pageParamsVO, sale);
  }

  @SysLog("导出商品统计")
  @GetMapping("/sku/export")
  @ApiOperation("导出商品统计")
  @IgnoreResponseEntityWrap
  @PreAuthorize("@p.check('sale:list')")
  public void exportSku(PageParamsVO pageParamsVO, SaleStatisticsParamVO sale, HttpServletResponse response) throws IOException {
    byte[] body = this.skuStatisticsService.export(pageParamsVO, sale);
    String fileName = System.getProperty("java.io.tmpdir") + File.separator + System.currentTimeMillis() + ".xls";
    FileUtil.downloadFile(this.request, response, FileUtil.writeBytes(body, fileName), true);
  }
}
